    <?php 
    function singleGenealogy($parent_level, $level = 0, $levelIds)  {
    	?>
    	
    	<?php 
    	if ($parent_level != null){
	       $children = Doctrine_Core::getTable('Level')->getLevelChildren($parent_level->getId());
	       if(count($children) > 0) {
	        	$level++;
		       	foreach ($children as $child) {
		       		$children_count = Doctrine_Core::getTable('Level')->countLevelChildren($parent_level->getId());
		       		$class = $children_count > 0 ? "jstree-open" : "jstree-leaf jstree-last";
		       		?>
		       		<li id="<?php echo $child->getId();?>" class = "<?php echo $class; ?>" >
		       		<a href="#">
		       		<?php $checked = $child->isMemberOfSelection($levelIds) ? "checked = 'checked'" : "" ?>
		       		<?php 
		       		
		             echo '<input '. $checked.' class="ignore" type="checkbox" name="check_'. $child->getId().'" id="check_'. $child->getId().'" />' . '<span class="tree_caption">' . $child->getName() . '</span>';
		             
		             ?>
		             </a>
		             <ul id="ul_<?php echo $child->getId()?>_<?php echo $level?>">
		             <?php 
		             singleGenealogy($child, $level,$levelIds);
		             ?>
		             </ul>
		             </li>
		             <?php 
		       	}
	       }
    	}
       ?>
       <?php 
    }
    
    ?>
    <div class="head"><h5 class="iList">Template Definition for <?php echo $directory->getName()?></h5></div>
     <form action="<?php echo url_for('templates/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" id="valid" class="mainForm" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
     	<input name="template[footer_id]" type="hidden" value="<?php echo $form->getObject()->getFooterId()?>" >
     	<div class="rowElem noborder">
     	<label for="name">Name:</label>
     		<div class="formRight"><input type="text" name="template[name]" value="<?php echo $form->getObject()->getName()?>"  class="validate[required]"  id="template_name"/></div>
     		<div class="fix"></div>
     	</div>
     	
     	<div class="rowElem noborder">
     		<span style="margin-left: 3px;">Include comments</span>
			<?php $checked = $form->getObject()->getShowComments() == true ? "checked" : ""; ?>
			<input type="checkbox" id="template_show_comments" name="template[show_comments]" <?php echo $checked; ?> value="1"  />
     		<div class="formRight">
			</div>
     		<div class="fix"></div>
     	</div>       	
     	
     	<?php $comments_box_show = $checked == "" ? "none" : ""?>
     	<div id="comments_box" style="display:<?php echo $comments_box_show?>">
	     	<div class="rowElem noborder">
	     	<label style="width:152px" for="name">Comment header:</label>
	     		<div class="formRight"><input type="text" name="template[comment_header]" value="<?php echo $form->getObject()->getCommentHeader()?>"  id="template_comment_header"/></div>
	     		<div class="fix"></div>
	     	</div>
	     		<div class="fix"></div>     	
	     	
	     	<div class="rowElem noborder">
	     	<label style="width:152px" for="name">Comment button caption:</label>
	     		<div class="formRight"><input type="text" name="template[comment_button_caption]" value="<?php echo $form->getObject()->getCommentButtonCaption()?>"  id="template_comment_button_caption"/></div>
	     		<div class="fix"></div>
	     	</div>
	     		<div class="fix"></div>
	     		
	     	<div class="rowElem noborder">
	     	<label style="width:152px" for="name">Comment login description:</label>
	     		<div class="formRight"><textarea name="template[comment_login_description]" id="template_comment_login_description"><?php echo $form->getObject()->getCommentLoginDescription()?></textarea></div>
	     		<div class="fix"></div>
	     	</div>
	     		<div class="fix"></div>     	
	     		
				<div class="rowElem noborder">
		     		<span style="margin-left: 3px;">Include comments rating</span>
					<?php $checked = $form->getObject()->getShowRating() == true ? "checked" : ""; ?>
					<input type="checkbox" id="template_show_rating" name="template[show_rating]" <?php echo $checked; ?> value="1" />
		     		<div class="formRight">
					
					</div>
		     		<div class="fix"></div>
		     	</div>     
     	
		     	<?php $comments_rating_box = $checked == "" ? "none" : ""?>
		     	<div id="comments_rating_box" style="display:<?php echo $comments_rating_box?>">
			     	<div class="rowElem noborder">
			     	<label style="width:152px" for="name">Rating caption:</label>
			     		<div class="formRight"><input type="text" name="template[rate_txt]" value="<?php echo $form->getObject()->getRateTxt()?>"  id="template_rate_txt"/></div>
			     		<div class="fix"></div>
			     	</div>
			     		<div class="fix"></div>     	   	
			 	
			     	<div class="rowElem noborder">
			     	<label style="width:152px" for="name">Order ratings by:</label>
			     		<div class="formRight">
			                    			<select name="template[rating_sorting_id]" id="template_rating_sorting" style="width:240px;" >
			                    				<?php 
			                    					
			                    					foreach ($sorting_criterias as $sorting_criteria) {
					                    					 $sorting_criteria_selected = $sorting_criteria->getId() == $form->getObject()->getRatingSortingId() ? "selected" : "";
					                    					?>
					                    						<option <?php echo $sorting_criteria_selected?> value="<?php echo $sorting_criteria->getId(); ?>"><?php echo $sorting_criteria->getCriteriaName(); ?></option>
					                    					<?php 
			                    					}
			                    				?>
			                            	</select>      		
			     		</div>
			     		<div class="fix"></div>
			     	</div>
			     	
		     	</div>	     			
     	</div>	
   	
     	
     	
     		<div class="fix"></div>   
     		 	
     	
     	<div class="rowElem noborder">
	     	     		
     		<input type="hidden" name="directory_id" id="directory_id" value="<?php echo $selected_directory ?>">
     		
     		<input type="hidden" name="template[alias_when_footer]" id="template_alias_when_footer" value="<?php echo $form->getObject()->getAliasWhenFooter() ?>">
     		
     		<div style="clear:both">
     		<?php
			
     			$home_directory = $form->getObject()->getMasterDirectory();
     			
     			$selectedDirectory = Doctrine_Core::getTable('MasterDirectory')->find(array($selected_directory)) ; 
     			
     			$show_categories = true;
     			
     			if ($selectedDirectory->getHomeTemplateId() == null || ($home_directory != null && $home_directory[0]->getId() != null )){	
     				$checked = $selectedDirectory->getHomeTemplateId() != null && $selectedDirectory->getHomeTemplateId() ==  $form->getObject()->getId() ? 'checked' : "";
     				$show_categories = false;
	     		?>
	     		<input <?php echo $checked?> type="checkbox" class="ignore" value="home_page" id="chb_home_page" name="chb_home_page"><span style="bottom: 2px;left: 5px;position: relative;"> Home page</span>
	     		<?php 
     			}
     			?>
     		</div>
     		<div class="fix"></div>
     	</div>
     	
     	<?php $display_cat = $show_categories ? "" : "display: none"?>
     
    <div style="clear:both; <?php echo $display_cat?>">
		<div class="our_subtitle" style="float:left">
			Involved levels
		</div>
		<div class="our_dashed" style="float: left; height: 1px; margin-top: 10px; width: 690px;">
		
		</div>
	</div>
	<div style="clear: both; width: 824px; <?php echo $display_cat ?>">
			<?php 
			$i = 0;
			foreach ($categories as $category) {
					$i++;
					$root_level = Doctrine_Core::getTable('Level')->getRootLevel($category->getId());
		       		?>
		       		
		       		<div style="float:left; margin-left: 20px;margin-top: 20px">
				    <div class="head"><h5 class="iList"><?php echo $category->getName()?> Levels Tree</h5></div>
				    <?php if ($root_level != null) {?>
				    <div  id="tree_<?php echo $category->getId()?>" name="root_div" class="demo">
				    	<ul>
				    		<li id="<?php echo $root_level->getId();?>" class="jstree-open">
								<?php $checked = $root_level->isMemberOfSelection($levelIds) ? "checked = 'checked'" : "" ?>
								<a href="#"><?php echo '<input type="checkbox" ' . $checked . '  class="ignore"  name="check_'. $root_level->getId().'" id="check_'. $root_level->getId().'" />' .'<span class="tree_caption">' . $root_level->getName(); ?></span></a>
								<ul id="ul_<?php echo $category->getId()?>_1">
						    	<?php 
						    		singleGenealogy($root_level,1,$levelIds);
						    	?>
						    	</ul>
					    	</li>
						</ul>
				    </div>
				    <?php }?>		       		
		       		</div>
		       		
		       		
     				<?php 
       				if ($i % 3 == 0 ){
       					?>
       					<div style="clear: both;"></div>
       					<?php 
       				}?>
		       		<?php 
		       }?>         		


     </div>
     	
     	<div class="fix"></div>
     	
        <input type="button" value="Save" class="greyishBtn submitForm" id="save" />
        
     	<?php if (!$form->getObject()->isNew()){
     		?>
     		<input type="button" value="Create New" class="greyishBtn submitForm" id="create_new" />
     	<?php 
     	} ?>
     	        
        <div class="fix"></div>
     </form>
     
     
     
<script type="text/javascript">

$("#save").click(function (event){
    submitForm();
})


$("#template_show_comments").click(function (event){
    if($("#template_show_comments").attr("checked") == "checked"){
		$("#comments_box").hide('slow');
    }
    else{
    	$("#comments_box").show('slow');
    }
})

$("#template_show_rating").click(function (event){
    if($("#template_show_rating").attr("checked") == "checked"){
		$("#comments_rating_box").hide('slow');
    }
    else{
    	$("#comments_rating_box").show('slow');
    }
})


$("form#valid").submit(function (event){
    event.preventDefault();
    submitForm();
})

$("#create_new").click(function (event){

    $.ajax({type: 'POST', url:' <?php echo url_for('templates/index')?>', script:true, cache:false,
            success: function(data) {
                 $('#template_super_container_div').html(data);
                 $('#template_name').attr('value',"");
             } });
             
})



<?php foreach ($categories as $category) { ?>
    	$("#tree_<?php echo $category->getId()?>")
    		.jstree({ "plugins" : ["themes","html_data","ui"]
        	 })
    		.bind("select_node.jstree", function (event, data) { 
				var li_id = data.rslt.obj.attr("id");
				
				//alert($('#check_' + li_id).attr("checked"));
				$('#check_' + li_id).attr("checked", !$('#check_' + li_id).attr("checked"));
    		})
    		// 2) if not using the UI plugin - the Anchor tags work as expected
    		//    so if the anchor has a HREF attirbute - the page will be changed
    		//    you can actually prevent the default, etc (normal jquery usage)
    		.delegate("a", "click", function (event, data) { event.preventDefault(); });        	 
    	  
<?php }?>

function submitForm(){
    var str=$('form#valid').serialize();

    $.ajax({type: 'POST', url:' <?php echo url_for('templates/'.($form->getObject()->isNew() ? 'create' :'update').
    (!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : ''))?>', script:true, cache:false, data:  str, 
    beforeSend: function(){
	        if ($("#template_name").attr("value") == ""){ 
		          return false;
        	}
			if ($("#chb_home_page").length){
				if ($("#chb_home_page").attr("checked") == false && $('input:checked', $('form#valid')).length == 0){
					return false;
				}
			}
			else if ($('input:checked', $('form#valid')).length == 0){
				
				return false;
			}
        	

		//validate that only a brother is marked
		var parent_arr = new Array();
        $("input:checked:not('#chb_home_page, #template_show_comments')").each(function()
        		{
    				var chb_id = $(this).attr("id");
    				var temp_arr = chb_id.split("_");
    				var li_id = temp_arr[1];
    				if ($('#'+li_id).parent().attr("id") != "" && $('#'+li_id).parent().attr("id") != null){
	    				parent_arr.push($('#'+li_id).parent().attr("id"));
    				}
    				
        		    // add $(this).val() to your array
        			//parent_arr.push($(this).parent().attr("id"));
        		});

			    var sorted_arr = parent_arr.sort(); // You can define the comparing function here. 
    
				var found = false;
				var index = 0;
				while(!found && index < parent_arr.length){
					if (sorted_arr[index + 1] == sorted_arr[index]){
						found = true;
					}
					index++;
				}
				if (found == true){
					return false;
				}
				
        },

            success: function(data) {
                 $('#template_super_container_div').html(data);
                 $('#template_name').attr('value',"");
               	 
             } });
}

		
		$('form').jqTransform({imgPath:'../images/forms'});

		function loadDirectoryData (){

			var php_url = '<?php echo url_for('templates/getTotalXml?directory_id=')?>';
			var directory_id = document.getElementById('directory_id').value;
			php_url = php_url.concat(directory_id);	
			
			$.ajax({type: 'POST', url:php_url, script:true, cache:false, 
				success: function(data) { 
					 $('#template_super_container_div').html(data);
					 $('#templates_name').attr('value',"");
					} });
			
		}

		
</script>

<style>
	.jqTransformCheckboxWrapper{
	 	
	    margin-top: 0px !important;
	   
	}
	
	.jstree a .jstree-icon { display:none !important; }
	
	 .tree_caption{
	    margin-left: 3px;
	    position: relative;
	    top: -2px;	 	
	 }
</style>
  
